package com.csxy.six.controller;

import com.csxy.six.R;
import com.csxy.six.entity.GoodsCategory;
import com.csxy.six.service.GoodsCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "goods-category", description = "商品分类管理")
@RestController
@RequestMapping("/market/categories")
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Operation(summary = "根据id查询商品分类", description = "根据id查询商品分类")
    @GetMapping("/getById/{id}")
    public R getCategoryById(@Parameter(name = "id", description = "商品分类ID", required = true) @PathVariable Integer id) {
        GoodsCategory category = goodsCategoryService.getById(id);
        return R.ok().data("item", category);
    }

    @Operation(summary = "查询所有商品分类", description = "查询所有商品分类")
    @GetMapping("/findAll")
    public R getAllCategories() {
        List<GoodsCategory> list = goodsCategoryService.list();
        return R.ok().data("items", list);
    }

    @Operation(summary = "新增商品分类", description = "新增商品分类")
    @PostMapping("/save")
    public R save(@RequestBody GoodsCategory category) {
        boolean flag = goodsCategoryService.getBaseMapper().insert(category) > 0;
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @Operation(summary = "修改商品分类", description = "修改商品分类")
    @PostMapping("/update")
    public R update(@RequestBody GoodsCategory category) {
        boolean flag = goodsCategoryService.updateById(category);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @Operation(summary = "删除商品分类", description = "根据id删除商品分类")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable Integer id) {
        boolean flag = goodsCategoryService.removeById(id);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
